package com.east.controller.system;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.east.bean.Privilege;
import com.east.bean.Role;
import com.east.service.system.MenuService;
import com.east.utils.PageJson;
import com.east.utils.Pagenation;
import com.east.utils.Result;
import com.east.utils.ResultUtil;
import com.github.pagehelper.Page;

@RestController
@RequestMapping("menu")
public class MenuController {
		@Autowired
		MenuService menuService;
	/*@GetMapping("/")
	public Result findBUserId() {
		List<Menu> findByUserId = menuService.findByUserId(null);
		return ResultUtil.success(findByUserId);
	}	*/
	/**
	 * 查询所有角色
	 * @return
	 */
	/*@GetMapping("/role")
	@RequiresPermissions("menu:role")
	public Result findByRole() {
		List<Role> findByUserId = menuService.findByRole();
		return ResultUtil.success(findByUserId);
	}	*/
	
	//------------------------------栏目管理----------------》》》》》》》
	
	/**
	 * 根据角色查询权限
	 * @return
	 */
	@GetMapping("/privRoleId")
	@RequiresPermissions("menu:privRoleId")
	public Result findByPrivId(Integer roleId) {
		List<Privilege> findByUserId = menuService.findByRolePriv(roleId);
		return ResultUtil.success(findByUserId);
	}
	/**
	 * 查询所有权限
	 * @return
	 */
	@GetMapping("/privAll")
	@RequiresPermissions("menu:privAll")
	public Result findByPrivAll() {
		List<Privilege> findByUserId = menuService.findByPrivAll();
		return ResultUtil.success(findByUserId);
	}	
	/**
	 * 查询所有有权限的栏目  去除顶级栏目
	 * @return
	 */
	@GetMapping("/privRoleAll")
	@RequiresPermissions("menu:privRoleAll")
	public Result findByprivRoleAll() {
		List<Privilege> findByUserId = menuService.findByprivRoleAll();
		return ResultUtil.success(findByUserId);
	}	
	
	/**
	 * 栏目权限添加
	 * @param privilege
	 * @return
	 */
	@PostMapping("/privAdd")
	@RequiresPermissions("menu:privAdd")
	public Result findByprivAdd(Privilege privilege) {
		int findByUserId = menuService.savePrivilege(privilege);
		if(findByUserId>0) {
			return ResultUtil.success("添加成功");
		}else {
			return ResultUtil.success(500,"添加失败");
		}
	
	}	
	/**
	 * 栏目权限修改
	 * @param privilege
	 * @return
	 */
	@PostMapping("/privUpd")
	@RequiresPermissions("menu:privUpd")
	public Result findByprivUpd(Privilege privilege) {
		int findByUserId = menuService.updatePrivilege(privilege);
		if(findByUserId>0) {
			return ResultUtil.success("修改成功");
		}else {
			return ResultUtil.success(500,"修改失败");
		}
	}	
	/**
	 * 栏目权限删除
	 * @param privilege
	 * @return
	 */
	@DeleteMapping("/privDel")
	@RequiresPermissions("menu:privDel")
	public Result findByprivDel(Privilege privilege) {
		int findByUserId = menuService.delPrivilege(privilege);
		if(findByUserId>0) {
			return ResultUtil.success("删除成功");
		}else {
			return ResultUtil.success(500,"删除失败");
		}
	}	
	/**
	 * 栏目权限详情
	 * @param privilege
	 * @return
	 */
	@GetMapping("/privId")
	@RequiresPermissions("menu:privId")
	public Result findByprivId(Privilege privilege) {
		Privilege findByUserId = menuService.findByPrivilegeId(privilege);
			return ResultUtil.success(findByUserId);
	}
	
	
	
	//------------------------------角色管理------------------------》》》》》》》
	/**
	 * 角色全查
	 * @param key
	 * @return
	 */
	@GetMapping("/roleAll")
	@RequiresPermissions("menu:roleAll")
	public Result findByroleAll(String key,Pagenation pagenation) {
		 Page<Role> findByRole = menuService.findByRole(pagenation);
		 Object pageJson = new PageJson<Role>().pageJson(findByRole);
		 return ResultUtil.success(pageJson);
	}
	/**
	 * 角色详情
	 * @param role
	 * @return
	 */
	@GetMapping("/roleId")
	@RequiresPermissions("menu:roleId")
	public Result findByroleId(Role role) {
		System.out.println(role.getId());
		    Role findByUserId = menuService.findByRolePrivilege(role);
			return ResultUtil.success(findByUserId);
	}
	/**
	 * 添加角色
	 * @param role
	 * @return
	 */
	@PostMapping(value="/roleAdd")
	@RequiresPermissions("menu:roleAdd")
	public Result findByroleAdd(String pIds,String name,String remarks) {
		System.out.println(pIds);
		String[] split = pIds.split(",");
		int findByUserId = menuService.saveRole(split, name, remarks);
		if(findByUserId>0) {
			return ResultUtil.success("添加成功");
		}else {
			return ResultUtil.success(500,"添加失败");
		}
	}
	/**
	 * 修改角色
	 * @param role
	 * @return
	 */
	@PostMapping("/roleUpd")
	@RequiresPermissions("menu:roleUpd")
	public Result findByroleUpd(String pIds,String name,String remarks,Integer id) {
		String[] split = pIds.split(",");
		int updateRole = menuService.updateRole(split, name, remarks, id);
		if(updateRole>0) {
			return ResultUtil.success("修改成功");
		}else {
			return ResultUtil.success(500,"修改失败");
		}
	}
	/**
	 * 删除角色
	 * @param role
	 * @return
	 */
	@DeleteMapping("/roleDel")
	@RequiresPermissions("menu:roleDel")
	public Result findByroleDel(Role role) {
		int findByUserId = menuService.delRole(role.getId());
		if(findByUserId>0) {
			return ResultUtil.success("删除成功");
		}else {
			return ResultUtil.success(500,"删除失败");
		}
	}
	
	
}
